#!/usr/bin/env python
# coding=utf-8

"""
Check A0000001.txt.
"""

import os
import sys
import re
import codecs

import CheckLog
log = CheckLog.get_file_log(r'checkA.log')

class DicItemA:
	def __init__(self, s):
		s = s.strip()
		self._linestr = s
		pat = re.match( \
				r'^(\S+)\t([\w ]+)\t(0x[\dA-F]{4})\t(0x[\dA-F]{4})\t(0x[\dA-F]{8})$', \
				self._linestr)
		if not pat:
			log.warning(r'<String Match Error> : %s : %s', self.get_father()['nextLevelId'], self.get_str())
			return
		self._name = pat.group(1)
		self._pinyin = pat.group(2)
		self._minKiwiId = pat.group(3)
		self._maxKiwiId = pat.group(4)
		self._num0 = pat.group(5)
		
	def get_str(self):
		return self._linestr
	
	def __getitem__(self, attr):
		if attr == 'name':
			return self._name
		elif attr == 'pinyin':
			return self._pinyin
		elif attr == 'minKiwiId':
			return self._minKiwiId
		elif attr == 'maxKiwiId':
			return self._maxKiwiId
		elif attr == 'num0':
			return self._num0
		else:
			raise KeyError()
		
	def check(self):
		""" Check the item itself only.
		"""
		checker = DicItemAChecker(self)
		r =  checker.check()
		if not r:
			log.warning(r'<Error> : %s', self.get_str())
		return r
	
class DicItemAChecker():
	pinyinChars = set(u'abcdefghijklmnopqrstuvwxyz /1234')
	
	def __init__(self, item):
		self._item = item
		
	def check(self):
		r1 = self._check_1()
		r2 = self._check_2()
		return r1 and r2
	
	def _check_1(self):
		"""Check encoding, which is checked in function check().
		"""
		return True
	
	def _check_2(self):
		"""Check pinyin.
		"""
		d = self._dicItem
		r1 = self._check_2_1()
		r2 = self._check_2_2();
		if not r1:
			log.info(r'<Check_2 Error> Alphabet text but not NoPinYin : %s', self._dicItem.get_str())
			return False
		if not r2:
			log.info(r'<Check_2 Error> Pinyin contains other characters : %s', self._dicItem.get_str())
			return False
		return True
	
	def _check_2_1(self):
		"""Check if text has alphabet, then pinyin should be "NoPinYin".
		"""
		def has_alphabet(s):
			for c in s:
				if c in self.alphabets:
					return True
			return False
		if has_alphabet(self._dicItem['name']):
			if self._dicItem['pinyin'] != u'NoPinYin':
				return False
		return True
	
	def _check_2_2(self):
		"""Check characters in pinyin.
		"""
		pinyin = self._dicItem['pinyin']
		if pinyin == 'NoPinYin':
			return True
		for c in pinyin:
			if not c in self.pinyinChars:
				return False
		return True
	
def check(filename):
	"""Check records in file.
	"""
	f = codecs.open(filename, 'r', 'gbk')
	try:
		ls = f.readlines()
	except:
		log.info(r'<Check Encoding Error> Contain non-GBK character : %s', self._dicItem.get_str())
		return False
	rds = tuple(DicItemA(line) for line in ls)
	r = tuple(r.check() for r in rds)
	return all(r)

def main():
	if len(sys.argv) != 2:
		print('Usage:')
		print(r'python check.py [Filepath of A000000.txt]')
		sys.exit(0)
	filename = sys.argv[1]
	check(filename)